package com.jdrx.sw.sewage.api.web;

import com.jdrx.platform.commons.rest.beans.vo.ResponseVO;
import com.jdrx.platform.commons.rest.factory.ResponseFactory2;
import com.jdrx.sw.sewage.beans.dto.MessageSubscribeDTO;
import com.jdrx.sw.sewage.beans.vo.MessageSubscribeVO;
import com.jdrx.sw.sewage.common.constant.Constant;
import com.jdrx.sw.sewage.common.enums.MessageSubscribeType;
import com.jdrx.sw.sewage.service.MessageSubscribeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.jdrx.platform.common.support.gateway.GwConstants.TRANSPARENT_USERID_FEILD;

@RestController
@RequestMapping(value = "/api/message")
@Api(value = "消息订阅")
public class MessageSubscribeApi {

    public static final Logger logger = LoggerFactory.getLogger(MessageSubscribeApi.class);

    @Autowired
    private MessageSubscribeServiceImpl messageSubscribeServiceImpl;

    @ApiOperation(value = "获取订阅列表")
    @GetMapping(value = "getList")
    public ResponseVO<List<MessageSubscribeVO>> getList(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                        @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                        @RequestHeader(TRANSPARENT_USERID_FEILD) Long uid,
                                                        @RequestParam("stationIds") List<Long> stationIds) {
        return ResponseFactory2.ok(messageSubscribeServiceImpl.getList(uid, stationIds));
    }

    @ApiOperation(value = "订阅")
    @PostMapping(value = "subscribe")
    public ResponseVO subscribe(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                @RequestHeader(Constant.TENANT_ID) String tenantId,
                                @RequestHeader(TRANSPARENT_USERID_FEILD) Long uid,
                                @RequestBody List<MessageSubscribeDTO> dto) {
        messageSubscribeServiceImpl.subscribe(dto);
        return ResponseFactory2.ok();
    }

    @ApiOperation(value = "获取用户微信openid")
    @GetMapping(value = "getOpenId")
    public ResponseVO getOpenId(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                @RequestHeader(Constant.TENANT_ID) String tenantId,
                                @RequestHeader(TRANSPARENT_USERID_FEILD) Long uid,
                                @RequestParam("jsCode") String jsCode) throws Exception {
        return ResponseFactory2.ok(messageSubscribeServiceImpl.getOpenId(jsCode, uid));
    }

    @ApiOperation(value = "告警和运营简报推送测试接口-勿调用！")
    @GetMapping(value = "pushAlarm")
    public ResponseVO pushAlarm(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                @RequestHeader(Constant.TENANT_ID) String tenantId) throws Exception {
        messageSubscribeServiceImpl.pushAlarm("o-Gis5bjgF58bhqZZ5PhBmNZQZ7E",
                975L,
                "告警详情",
                "告警类型告警通知",
                "请尽快处理，点击查看详情",
                MessageSubscribeType.ALARM_EVENT_PUSH.getKey());
        messageSubscribeServiceImpl.pushAlarm("o-Gis5bjgF58bhqZZ5PhBmNZQZ7E",
                null,
                "04月26日的报告已生成，水质达标XX%，当日出现XX告警",
                "运营日报通知",
                "简报已生成，点击查看详情",
                MessageSubscribeType.BRIEF_REPORT_PUSH.getKey());
        return ResponseFactory2.ok();
    }
}
